function postToS3(msg, file, success, fail) {
    var formData = verifyAndCreateFormData2AWS(msg, file);

    if (formData == null) {
        return;
    }
    debugAlert("try post file : " + msg.filename);
    var Ghost = "https://"+msg.bucket+".s3.cn-north-1.amazonaws.com.cn";

    $.ajax({
        url: Ghost,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        timeout: 30000,
        beforeSend : function(XHR) {
            loadingCount++;
            //debugAlert("loadingCount add " + loadingCount );
            buildLoadingDiv();
            loadingDiv.removeClass("hide");
            maskLoading.removeClass("hide");
        },
        complete: function(XHR,TS) {
            if(--loadingCount == 0) {
                //alert("before remove");
                loadingDiv.addClass("hide");
                maskLoading.addClass("hide");

                networkErrorAlertTime = 0;
            }
            //debugAlert("loadingCount remove" + loadingCount );

            if(networkErrorAlertTime == 0) {
                if (TS == "error") {
                    alert("请检查是否正确连接到网络！");
                    //alert(location);
                    //alert(XMLHttpRequest.responseText);

                    networkErrorAlertTime++;
                }
                if (TS == "timeout") {
                    alert("网络不给力，请稍后再次尝试！");
                    networkErrorAlertTime++;
                }
            }
        },
        success: function () {
            success(Ghost+"/"+msg.key);
        } ,
        error : fail
    });
}


function postImageToS3(msg, blobFile, uri, success, fail) {
    var formData = verifyAndCreateFormData2AWS(msg, blobFile);

    if (formData == null) {
        return;
    }
    debugAlert("try post file : " + msg.filename);
    var Ghost = "https://"+msg.bucket+".s3.cn-north-1.amazonaws.com.cn";

    $.ajax({
        url: Ghost,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        timeout: 120000,
        beforeSend : function(XHR) {
            loadingCount++;
            //debugAlert("loadingCount add " + loadingCount );
            buildLoadingDiv();
            loadingDiv.removeClass("hide");
            maskLoading.removeClass("hide");
        },
        complete: function(XHR,TS) {
            if(--loadingCount == 0) {
                //alert("before remove");
                loadingDiv.addClass("hide");
                maskLoading.addClass("hide");

                networkErrorAlertTime = 0;
            }
            //debugAlert("loadingCount remove" + loadingCount );

            if(networkErrorAlertTime == 0) {
                if (TS == "error") {
                    alert("请检查是否正确连接到网络！");
                    //alert(location);
                    //alert(XMLHttpRequest.responseText);

                    networkErrorAlertTime++;
                }
                if (TS == "timeout") {
                    alert("网络不给力，请稍后再次尝试！");
                    networkErrorAlertTime++;
                }
            }
        },
        success: function () {
            success(Ghost+"/"+msg.key, uri);
        } ,
        error : fail
    });
}


/**
 * 同步上传
 * @param msg
 * @param blobFile
 * @param uri
 * @param success
 * @param fail
 */
function postImageToS3Syn(msg, blobFile, uri, success, fail) {
    var formData = verifyAndCreateFormData2AWS(msg, blobFile);

    if (formData == null) {
        return;
    }
    debugAlert("try post file : " + msg.filename);
    var Ghost = "https://"+msg.bucket+".s3.cn-north-1.amazonaws.com.cn";

    $.ajax({
        url: Ghost,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        timeout: 30000,
        async:false,
        beforeSend : function(XHR) {
            loadingCount++;
            //debugAlert("loadingCount add " + loadingCount );
            buildLoadingDiv();
            loadingDiv.removeClass("hide");
            maskLoading.removeClass("hide");
        },
        complete: function(XHR,TS) {
            if(--loadingCount == 0) {
                //alert("before remove");
                loadingDiv.addClass("hide");
                maskLoading.addClass("hide");

                networkErrorAlertTime = 0;
            }
            //debugAlert("loadingCount remove" + loadingCount );

            if(networkErrorAlertTime == 0) {
                if (TS == "error") {
                    alert("请检查是否正确连接到网络！");
                    //alert(location);
                    //alert(XMLHttpRequest.responseText);

                    networkErrorAlertTime++;
                }
                if (TS == "timeout") {
                    alert("网络不给力，请稍后再次尝试！");
                    networkErrorAlertTime++;
                }
            }
        },
        success: function () {
            success(Ghost+"/"+msg.key, uri);
        } ,
        error : fail
    });
}

function verifyAndCreateFormData2AWS(msg, file) {
    var formData = new FormData();

    formData.append('key', msg.key);
    formData.append('acl', msg.acl);
    formData.append('x-amz-meta-uuid', msg.uuid);
    formData.append('X-Amz-Credential', msg.credential);
    formData.append('X-Amz-Algorithm', msg.algorithm);
    formData.append('X-Amz-Date', msg.date);
    formData.append('x-amz-meta-tag', msg.tag);
    formData.append('Policy', msg.policy);
    formData.append('X-Amz-Signature', msg.signature);
    formData.append('file', file);

    return formData;
}

/**
 * 将以base64的图片url数据转换为Blob
 * @param urlData
 *            用url方式表示的base64图片数据
 */
function convertBase64UrlToBlob(urlData){

    var bytes=window.atob(urlData.split(',')[1]);        //去掉url的头，并转换为byte

    //处理异常,将ascii码小于0的转换为大于0
    var ab = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
    }

    return new Blob( [ab] , {type : 'image/png'});
}
//阿里云OSS图片上传
function uploadImageToAli(oss,uploadFile,context){
    var host = getCookie("lucy.host"),
    	httpsPort = getCookie("lucy.https.port"),
    	httpPort = getCookie("lucy.http.port");
//    var appServer = 'http://172.28.6.51:8088/mandy/getSTSToken.json';
    var appServer = 'http://' + location.host + ':' + location.port + '/mandy/getSTSToken';
    var bucket = 'twsz';
    var region = 'oss-cn-shanghai';

    var token = window.localStorage.getItem('token') ? window.localStorage.getItem('token') : '';
    if (!token) return;
    var OSS = oss;
    var urllib = OSS.urllib;
    var Buffer = OSS.Buffer;
    var OSS = OSS.Wrapper;
    var STS = OSS.STS;
    var targetId = context.getAttribute("id");
    function applyTokenDo(func) {
	var url = appServer;
	return urllib.request(url, {
	    method: 'GET',
	    data:'token='+token
	}).then(function (result) {
	    var creds = JSON.parse(result.data);
	    console.log(creds)
            var client = new OSS({
                region: region,
                accessKeyId: creds.accessKeyId,
                accessKeySecret: creds.accessKeySecret,
                stsToken: creds.securityToken,
                bucket: bucket
            });
            return func(client,targetId);
	});
    }; 
    applyTokenDo(uploadFile);
}